#define BLINKER_PRINT Serial
#define BLINKER_WIFI

#include <Blinker.h>

#include <ESP32Servo.h>

Servo myServo; // 创建舵机对象


#define REDLED1 12 // WIFI开始连接，亮灯，连接成功后，灯灭
#define REDLED2 13 // WIFI开始连接，亮灯，连接成功后，灯灭
bool isBlinking1 = false; // 标志变量，用于控制 LED 闪烁状态
bool isBlinking2 = false; // 标志变量，用于控制 LED 闪烁状态



char ssid[] = "TEmDrive";
char pswd[] = "";
char auth[] = "1db501e98b82"; // blinker app提供的秘钥DF1719CDWSHHQKU7P2KBDY1R
bool ledState1 = LOW;
bool ledState2 = LOW;


// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerButton Button2("btn-off");
BlinkerNumber Number1("num-abc");
BlinkerButton Button3("btn-1");
BlinkerButton Button4("btn-2");
BlinkerButton FlickerButton1("btn-start1");  // 替换为 Blinker APP 上“开始”按钮的键值
BlinkerButton FlickerButton2("btn-start2");  // 替换为 Blinker APP 上“开始”按钮的键值
BlinkerSlider Slider("ran-f33");
BlinkerButton ButtonOn("btn-on");
BlinkerSlider SliderServo("ran-p4e");



int counter = 0;

// 按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    digitalWrite(REDLED1, !digitalRead(REDLED1));
    digitalWrite(REDLED2, !digitalRead(REDLED2));
}


// 按下按键即会执行该函数 转换全部灯光状态
void button2_callback(const String & state)
{
    Button2.print(state);//开关状态返回app
    if (state == "on") {
      digitalWrite(REDLED1, HIGH);
      digitalWrite(REDLED2, HIGH);
    }else{
      digitalWrite(REDLED1, LOW);
      digitalWrite(REDLED2, LOW);
    }
}

// 转换1号灯
void button3_callback(const String & state)
{
  analogWrite(REDLED1, 0);
  BLINKER_LOG("get button3 state: ", state);
  Button3.print(state);//开关状态返回app
  if (state == "on") {
  digitalWrite(REDLED1, HIGH);
  }else{
  digitalWrite(REDLED1, LOW);
  }
}

// 转换2号灯
void button4_callback(const String & state)
{
  BLINKER_LOG("get button4 state: ", state);
  Button4.print(state);//开关状态返回app
  if (state == "on") {
  digitalWrite(REDLED2, HIGH);
  }else{
  digitalWrite(REDLED2, LOW);
  }
}

// 控制舵机开关机
void buttonOn_callback(const String & state)
{
  BLINKER_LOG("get buttonOn state: ", state);
  ButtonOn.print(state);//开关状态返回app
  if (state == "on") {
    myServo.write(0); // 设置舵机角度为0度
    delay(1000); // 等待1秒
  }else{
    myServo.write(60); // 设置舵机角度为0度
    delay(1000); // 等待1秒
  }
}



//1号灯闪烁
void FlickerButton1Callback(const String & state) {
  BLINKER_LOG("FlickerButton1Callback button state: ", state);
  if (state == "on") {
    isBlinking1 = true;
    FlickerButton1.print("on");
  }
  if (state == "off") {
    FlickerButton1.print("off");
    isBlinking1 = false;
    analogWrite(REDLED1, 0);  // 停止闪烁时关闭 LED
  }
}


//2号灯闪烁
void FlickerButton2Callback(const String & state) {
  BLINKER_LOG("FlickerButton2Callback button state: ", state);
  if (state == "on") {
    isBlinking2 = true;
    FlickerButton2.print("on");
  }
  if (state == "off") {
    FlickerButton2.print("off");
    isBlinking2 = false;
    digitalWrite(REDLED2, LOW);  // 停止闪烁时关闭 LED
  }
}
void SliderCallback(int32_t value){
  BLINKER_LOG("get slider value: ", value);//获取滑动条数值
Slider.print(value);
  		analogWrite(REDLED1,value);		//将value的值进行PWM输出
}

void SliderServoCallback(int32_t value){
  BLINKER_LOG("get SliderServoCallback value: ", value);//获取滑动条数值
  SliderServo.print(value);
  myServo.write(value);		//将value的值进行PWM输出
  delay(1000);
}


// 如果未绑定的组件被触发，则会执行其中内容
void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
    counter++;
    Number1.print(counter);
}

void setup()
{
    // 初始化串口
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    //BLINKER_DEBUG.debugAll();

    pinMode(REDLED1, OUTPUT);
    pinMode(REDLED2, OUTPUT);  
    myServo.attach(2); // 将舵机连接到GPIO引脚2

    Serial.println("Adafruit AHT10 demo!");

    digitalWrite(REDLED1, HIGH);
    // 初始化blinker
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);

    Button1.attach(button1_callback);
    Button2.attach(button2_callback);
    Button3.attach(button3_callback);
    Button4.attach(button4_callback);
    ButtonOn.attach(buttonOn_callback);
    FlickerButton1.attach(FlickerButton1Callback);
    FlickerButton2.attach(FlickerButton2Callback);
    Slider.attach(SliderCallback);
    SliderServo.attach(SliderServoCallback);
    digitalWrite(REDLED2, HIGH);
    Serial.println("启动成功");
    // analogWrite(REDLED1, 255);
    // analogWrite(REDLED2, 100);
}

void loop() {
    Blinker.run();
  if (isBlinking1) {
        digitalWrite(REDLED1, ledState1);
        delay(300);
        ledState1 = !ledState1;
  }
  if (isBlinking2) {
        digitalWrite(REDLED2, ledState2);
        delay(300);
        ledState2 = !ledState2;
  }
  
  // for (int i = 0; i < 255; i++) {
  //   analogWrite(REDLED1, i);

  //   delay(4);
  // }

  // for (int i = 255; i >= 0; i--) {
  //   analogWrite(REDLED1, i);

  //   delay(4);
  // }
  // for (int i = 0; i < 100; i++) {

  //   analogWrite(REDLED2, i);
  //   delay(4);
  // }

  // for (int i = 100; i >= 0; i--) {

  //   analogWrite(REDLED2, i);
  //   delay(4);
  // }
}

